﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DienLib.PopUp
{
    /// <summary>
    /// Interaction logic for PopUpWellType.xaml
    /// </summary>
    public partial class PopUpRetry : UserControl
    {
        DienLib.PopUp.PopUpResult _result = DienLib.PopUp.PopUpResult.NA;
        private PopUpRetry(string Comment, string Title)
        {
            InitializeComponent();
            Run run = new Run(textBlock1.Text); run.Foreground = Brushes.Black; textBlock1.Inlines.Clear(); textBlock1.Inlines.Add(new Bold(run));
            textBlock2.Inlines.Clear();
            textBlock1.Inlines.AddRange(PopUp.Command.HighlightTextBlockMatch(Title, "\"[^\"]*\"", null, true)); // "[^"]*" : Mean "*" any character with double-quotes
            textBlock2.Inlines.AddRange(PopUp.Command.HighlightTextBlockMatch(Comment, "\"[^\"]*\"", System.Windows.Media.Brushes.Blue, true));
            /*string[] split = System.Text.RegularExpressions.Regex.Split(Comment, @"(""[^""]*"")");
            foreach (string s in split)
            {
                if (s.Contains("\""))
                {
                    Run run = new Run(s); run.Foreground = System.Windows.Media.Brushes.Blue;
                    textBlock2.Inlines.Add(new System.Windows.Documents.Bold(run));
                }
                else textBlock2.Inlines.Add(new Run(s));
            }*/

            /*System.Text.RegularExpressions.MatchCollection matches = System.Text.RegularExpressions.Regex.Matches(Comment, @"(""[^""]*"")");
            if (matches.Count != 0)
            {
                TextEffectCollection collection = new TextEffectCollection(); // This markup Text using TextEffect
                foreach (System.Text.RegularExpressions.Match match in matches)
                {
                    collection.Add(new TextEffect(null, Brushes.Blue, null, match.Index, match.Length));
                }
                textBlock2.TextEffects = collection;
            }*/
        }

        /// <summary>
        /// Additional Method from User to Get Value back from PopUp
        /// </summary>
        /// <returns>String contain ShipTo Number</returns>
        public static DienLib.PopUp.PopUpResult ShowDialog(out bool DoNotShowAgain, string Comment, string Title = "N/A")
        {
            /*System.Windows.Controls.Primitives.Popup popup = new System.Windows.Controls.Primitives.Popup();
            popup.IsOpen = true;
            popup.StaysOpen = false;*/
            PopUpRetry popup = new PopUpRetry(Comment, Title);
            Window window = new Window();
            window.Content = popup;
            window.SizeToContent = System.Windows.SizeToContent.WidthAndHeight;
            window.WindowStyle = System.Windows.WindowStyle.None;
            window.AllowsTransparency = true;
            window.Background = System.Windows.Media.Brushes.Transparent;
            window.Topmost = true;
            window.WindowStartupLocation = WindowStartupLocation.CenterScreen;
            window.MouseLeftButtonDown += Window_MouseLeftButtonDown;
            System.Media.SystemSounds.Beep.Play();
            window.ShowDialog(); // ShowDialog can be used Internal ? HA HA HA ! Any Side Effect ?
            window.MouseLeftButtonDown -= Window_MouseLeftButtonDown;
            DoNotShowAgain = (bool)popup.checkBox1.IsChecked;
            return popup._result;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            this._result = PopUp.PopUpResult.RETRY;
            try { Window.GetWindow(this).Close(); }
            catch (Exception ex) { throw new InvalidOperationException("Cannot close Parent not Window : " + ex.Message); }
        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this._result = PopUp.PopUpResult.NEXT;
            try { Window.GetWindow(this).Close(); }
            catch (Exception ex) { throw new InvalidOperationException("Cannot close Parent not Window : " + ex.Message); }
        }

        private static void Window_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            (sender as System.Windows.Window).DragMove();
        }
    }
}
